import datetime
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
# #驱动程序管理的⾃动化
# #创建驱动对象
# # 1.打开浏览器
# driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# # 2.输⼊百度⽹址:https: // www.baidu.com
# driver.get("https://www.baidu.com")
# # 4、找到“百度⼀下”按钮并点击
#
# # 3、找到输⼊框并输⼊“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# # 5、关闭浏览器
# driver.quit()

# 1.打开浏览器
options=webdriver.ChromeOptions()
options.add_argument('-headless')
driver=webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=options)
driver.get("https://www.baidu.com/")
print(driver.title)
# driver.get("https://www.baidu.com/")
# ret=driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)
# # ele=driver.find_element(By.CSS_SELECTOR,'#su')
# # ele.click()
# # driver.find_element(By.CSS_SELECTOR,'#kw').click()
# driver.find_element(By.CSS_SELECTOR,'#kw').send_keys('比特')
# # driver.find_element(By.CSS_SELECTOR,'#su').click()
# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,'#kw').clear()
# driver.find_element(By.CSS_SELECTOR,'#kw').send_keys('比特')
#
# time.sleep(3)
# text=driver.find_element(By.CSS_SELECTOR,'#su').get_attribute('value')
# print(text)
# assert text=='百度一下'
# title=driver.title
# print(title)
# url=driver.current_url
# print(url)
# print('before: '+driver.title)
# print('before:'+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,'#s-top-left > a:nth-child(6)').click()
# time.sleep(3)
# print('after: '+driver.title)
# print('after:'+driver.current_url)
# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,'#s-top-left > a:nth-child(6)').click()
# curHandle=driver.current_window_handle
# allHandle=driver.window_handles
# for win in allHandle:
#     if win!=curHandle:
#         driver.switch_to.window(win)
# print('after: '+driver.title)
# print('after:'+driver.current_url)

# #设置窗口大小
# #最大化
# driver.maximize_window()
# time.sleep(3)
# #最小化
# driver.minimize_window()
# time.sleep(3)
# #全屏
# driver.fullscreen_window()
# time.sleep(3)
# #手动设置尺寸
# driver.set_window_size(1024,800)
# time.sleep(3)

#添加屏幕截图
#初级
# fileename="./images/test.png"
# driver.save_screenshot(fileename)
#高阶
# fileename='autotest-'+datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')+'.png'
# driver.save_screenshot('./images/'+fileename)
# print(datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S'))

# driver.find_element(By.CSS_SELECTOR,'#s-top-left > a:nth-child(6)').click()
# time.sleep(3)
# driver.close()
# time.sleep(3)

#隐式等待
# driver.implicitly_wait(3)

# driver.get("https://www.baidu.com/")
# driver.find_element(By.CSS_SELECTOR,'#kw').send_keys('爱坤')
# driver.find_element(By.CSS_SELECTOR,'#su').click()
# #添加显示等待
# #创建显示等待类对象
# wait= WebDriverWait(driver,2)
# wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'#\\36  > div > div:nth-child(1) > div:nth-child(1) > h3 > a')))
# # time.sleep(5)
# driver.find_element(By.CSS_SELECTOR,'#\\36  > div > div:nth-child(1) > div:nth-child(1) > h3 > a').click()

# driver.get("https://www.baidu.com/")
# driver.find_element(By.CSS_SELECTOR,'#kw').send_keys('爱坤')
# driver.find_element(By.CSS_SELECTOR,'#su').click()
# time.sleep(2)
# driver.back()
# time.sleep(2)
# driver.forward()
# time.sleep(2)
# driver.refresh()
driver.quit()